import { apiService } from './api';

// Dashboard相关类型定义
export interface DashboardStats {
  // 个人统计字段
  pendingTasks?: number;
  todayNewTasks?: number;
  todayCompletedTasks?: number;
  weeklyStats?: any;
  monthlyStats?: any;
  overdueStats?: any;
  
  // 全局统计字段
  totalPendingTasks?: number;
  totalTodayNewTasks?: number;
  totalTodayCompletedTasks?: number;
  globalWeeklyStats?: any;
  
  // 任务分类统计
  taskCategoryStats?: Array<{category: string; count: number}>;
  taskTypeStats?: Array<{type: string; count: number}>;
  
  // Top 10统计
  topCreators?: Array<{name: string; count: number}>;
  topAssignees?: Array<{name: string; count: number}>;
}

export interface RecentTask {
  id: string;
  name: string;
  status: string;
  assignee: string;
  dueDate: string;
  progress: number;
}

export interface Notification {
  id: string;
  title: string;
  content: string;
  type: string;
  time: string;
  read: boolean;
}

// Dashboard服务类
class DashboardService {
  // 获取Dashboard统计数据
  async getDashboardStats(): Promise<DashboardStats> {
    return apiService.get('/dashboard/stats');
  }

  // 获取最近任务
  async getRecentTasks(limit: number = 10): Promise<RecentTask[]> {
    return apiService.get('/dashboard/recent-tasks', { limit });
  }

  // 获取通知列表
  async getNotifications(params?: {
    page?: number;
    size?: number;
    read?: boolean;
  }): Promise<{
    content: Notification[];
    totalElements: number;
    totalPages: number;
    size: number;
    number: number;
  }> {
    return apiService.get('/dashboard/notifications', params);
  }

  // 标记通知为已读
  async markNotificationAsRead(id: string): Promise<void> {
    return apiService.patch(`/dashboard/notifications/${id}/read`);
  }

  // 标记所有通知为已读
  async markAllNotificationsAsRead(): Promise<void> {
    return apiService.patch('/dashboard/notifications/read-all');
  }

  // 删除通知
  async deleteNotification(id: string): Promise<void> {
    return apiService.delete(`/dashboard/notifications/${id}`);
  }

  // 获取任务完成率趋势
  async getCompletionTrend(params?: {
    userId?: string;
    days?: number;
  }): Promise<Array<{date: string; completionRate: number}>> {
    return apiService.get('/dashboard/completion-trend', params);
  }

  // 获取任务平均处理时间
  async getAverageProcessingTime(params?: {
    userId?: string;
    days?: number;
  }): Promise<{averageTime: number; unit: string}> {
    return apiService.get('/dashboard/average-processing-time', params);
  }

  // 获取过期任务统计
  async getOverdueStats(userId?: string): Promise<{
    totalOverdue: number;
    overdueByCategory: Array<{category: string; count: number}>;
  }> {
    return apiService.get('/dashboard/overdue-stats', userId ? { userId } : undefined);
  }

  // 获取系统健康状态
  async getSystemHealth(): Promise<{
    status: 'healthy' | 'warning' | 'error';
    services: {
      name: string;
      status: 'up' | 'down';
      responseTime?: number;
    }[];
    uptime: number;
    memoryUsage: number;
    cpuUsage: number;
  }> {
    return apiService.get('/dashboard/system-health');
  }
}

export const dashboardService = new DashboardService();
export default dashboardService;